package com.ctgu.controller;

import com.ctgu.service.UserService;
import com.ctgu.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/loginCheck")
public class UserController {
    @Resource
    UserService userService;

    //退出登录
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("msg");
        request.getSession().removeAttribute("userId");
        return "login";
    }

    //登录验证
    @RequestMapping("/sLogin")
    public String sloginCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(request.getParameter("sId") == "" || request.getParameter("sPass") == "") {
//            返回登录页面
            return "login";
        }else {
            int userid = Integer.parseInt(request.getParameter("sId"));
            String userpass = request.getParameter("sPass");
            User admin = userService.getById(userid);
            if(admin != null) {
                int type = admin.getType();
                if(admin.getPassword().equals(userpass)&&(type == 0)) {
                    request.getSession().setAttribute("msg", "登录成功");
                    request.getSession().setAttribute("userId", userid);
//                    request.getSession().setAttribute("mention", null);
//                    return "student/studentMain";
                    return "student/studentIndex";
                }else {
                    request.getSession().setAttribute("msg", "登陆失败");
                    return "login";
                }
            }else {
                request.getSession().setAttribute("msg", "用户不存在");
                return "login";
            }
        }
    }

    @RequestMapping("/tLogin")
    public String tloginCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(request.getParameter("tId") == "" || request.getParameter("tPass") == "") {
//            返回登录页面
            return "login";
        }else {
            int userid = Integer.parseInt(request.getParameter("tId"));
            String userpass = request.getParameter("tPass");
            User admin = userService.getById(userid);
            if(admin != null) {
                int type = admin.getType();
                if(admin.getPassword().equals(userpass)&&(type == 1)) {
                    request.getSession().setAttribute("msg", "登录成功");
                    request.getSession().setAttribute("userId", userid);
                    return "redirect:/teacher/allMyClass";
                }else {
                    request.getSession().setAttribute("msg", "登陆失败");

                    return "login";
                }
            }else {
                request.getSession().setAttribute("msg", "用户不存在");
                return "login";
            }
        }
    }



}
